//
//  JwSkiner.m
//  JwPack
//
//  Created by 陈警卫 on 2024-08-02.
//

#import "JwSkiner.h"

#define kJwSkinerKey @"kJwSkinerKey"

@implementation JwSkinerModel

/// 快捷创建
/// sel: _cmd
/// param: 字典 (对应 [JwSkiner shared].skinKeys作为字典key)
+ (JwSkinerModel *)modelWithSel:(SEL)sel param:(NSDictionary *)param {
    JwSkinerModel *model = [[JwSkinerModel alloc] init];
    model.key = NSStringFromSelector(sel);
    model.param = param;
    NSMutableArray *params = [NSMutableArray array];
    for (NSString *key in kJwSkiner.skinKeys) {
        [params addObject:(param[key] ?: @"")];
    }
    model.params = params;
    return model;
}

/// 快捷创建
/// sel: _cmd
/// params: 数组 (对应 [JwSkiner shared].skinKeys)
+ (JwSkinerModel *)modelWithSel:(SEL)sel params:(NSArray *)params {
    JwSkinerModel *model = [[JwSkinerModel alloc] init];
    model.key = NSStringFromSelector(sel);
    model.params = params;
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    for (NSInteger i = 0; i < kJwSkiner.skinKeys.count; i++) {
        NSString *key = kJwSkiner.skinKeys[i];
        NSString *value = @"";
        if (i < params.count - 1) {
            value = params[i];
        }
        param[key] = value;
    }
    model.param = param;
    return model;
}

/// MARK: 获取当前颜色
- (UIColor *)color {
    NSString *colorString = self.param[kJwSkiner.currentSkinKey];
    if (colorString) {
        if (kJwSkiner.skinKeys.count > 0) {
            NSString *key = kJwSkiner.skinKeys[0];
            colorString = self.param[key] ?: @"000000";
        }
    }
    UIColor *color = [UIColor jw_colorHexString:colorString];
    return color;
}

/// MARK: 获取当前值
- (NSString *)value {
    NSString *value = self.param[kJwSkiner.currentSkinKey];
    return value;
}



@end


@interface JwSkiner ()

/// 主题数组 默认@[@"any", @"light", @"dark"]
@property (nonatomic, strong) NSMutableArray *skinKeys;
/// 当前的主题key
@property (nonatomic, strong) NSString *currentSkinKey;
/// 当前的主题下标
@property (nonatomic, assign) NSInteger currentSkinIndex;

/// 主题数组 图片后缀别名 默认@[@"a", @"l", @"d"]
@property (nonatomic, strong) NSMutableArray *skinImageSuffixKeys;

@end

@implementation JwSkiner

/// 单例
+ (JwSkiner *)shared {
    static JwSkiner *sharedObject;
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        sharedObject = [[JwSkiner alloc] initShared];
    });
    return sharedObject;
}

- (instancetype)init {
    return [[self class] shared];
}

- (instancetype)initShared {
    self = [super init];
    if (self) {
        [self setupData];
    }
    return self;
}

- (void)setupData {
    self.skinKeys = [NSMutableArray arrayWithArray:@[@"any", @"dark", @"light"]];
    self.skinImageSuffixKeys = [NSMutableArray arrayWithArray:@[@"a", @"d", @"l"]];
    self.skinImageEx = JwSkinerImageExUnknow;
    
    self.currentSkinKey = [[NSUserDefaults standardUserDefaults] objectForKey:kJwSkinerKey];
    if ([self.skinKeys containsObject:self.currentSkinKey]) {
        self.currentSkinIndex = [self.skinKeys indexOfObject:self.currentSkinKey];
    }
    
    if (self.currentSkinKey == nil) {
        self.currentSkinKey = self.skinKeys[0];
        self.currentSkinIndex = 0;
        [self saveCurrentSkinKeyAndPostNoti];
    }
}

/// 设置主题数组
- (void)setupSkinKeys:(NSArray *)keys {
    if (keys.count > 0) {
        self.skinKeys = [NSMutableArray arrayWithArray:keys];
        self.currentSkinKey = self.skinKeys[0];
        self.currentSkinIndex = 0;
        [self saveCurrentSkinKeyAndPostNoti];
    }
}

/// 设置主题数组 图片后缀别名
- (BOOL)setupSkinImageSuffixKeys:(NSArray *)keys {
    if (keys.count != self.skinKeys.count) {
        NSLog(@"[JwSkiner] setupSkinImageSuffixKeys失败 keys.count与skinKeys.count不一致");
        return NO;
    }
    self.skinImageSuffixKeys = [NSMutableArray arrayWithArray:keys];
    return YES;
}

/// 当前主题图片后缀别名
- (NSString *)currentSkinImageSuffixKey {
    return self.skinImageSuffixKeys[self.currentSkinIndex];
}

/// 设置主题
- (BOOL)setupCurrentSkinKey:(NSString *)key {
    if (![self.skinKeys containsObject:key]) {
        NSLog(@"[JwSkiner] setupCurrentSkinKey失败 key不在skinKeys内");
        return NO;
    }
    self.currentSkinKey = key;
    self.currentSkinIndex = [self.skinKeys indexOfObject:key];
    [self saveCurrentSkinKeyAndPostNoti];
    return YES;
}

/// 设置主题
- (BOOL)setupCurrentSkinIndex:(NSUInteger)index {
    if (index >= self.skinKeys.count) {
        NSLog(@"[JwSkiner] setupCurrentSkinIndex失败 index不在skinKeys内");
        return NO;
    }
    NSString *key = self.skinKeys[index];
    self.currentSkinKey = key;
    self.currentSkinIndex = index;
    [self saveCurrentSkinKeyAndPostNoti];
    return YES;
}

/// 保存主题
- (void)saveCurrentSkinKeyAndPostNoti {
    [[NSUserDefaults standardUserDefaults] setObject:self.currentSkinKey forKey:kJwSkinerKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self postNoti];
}

/// 发送通知
- (void)postNoti {
    [[NSNotificationCenter defaultCenter] postNotificationName:kJwSkinerNotiKey object:self userInfo:nil];
}


/// 设置FrameProperty
- (void)setupFrameProperty {

    self.jw_theme = [self.colorClass jw_theme];
    self.jw_back = [self.colorClass jw_back];
    self.jw_title = [self.colorClass jw_title];
    self.jw_value = [self.colorClass jw_value];
}




@end

